package cn.wolfcode.ssm.web;

import java.util.List;

import org.apache.ibatis.type.MappedJdbcTypes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.wolfcode.ssm.domain.User;
import cn.wolfcode.ssm.service.IUserService;

@Controller
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	private IUserService userService;
	
	@RequestMapping("/list")
	public String listAll(Model model){
		List<User> list = userService.listAll();
		model.addAttribute("list", list);
		return "user/list";
	}
	
	@RequestMapping("/edit")
	public String Edit(Long id,Model model){
		if(id != null){
			User user = userService.get(id);
			model.addAttribute("user", user);
		}
		return "user/edit";
	}
	
	@RequestMapping("/saveOrUpdate")
	public String saveOrUpdate(User user){ 
		if(user.getId() == null){
			userService.save(user);
		}else{
			userService.update(user);
		}
		return "redirect:/user/list.do";
	}
	
	@RequestMapping("/delete")
	public String Delete(Long id){
		if(id != null){
			userService.delete(id);
		}
		return "redirect:/user/list.do";
	}
}
